#!/bin/sh
### TODO need improve since it takes 80 seconds
### TODO why date -d '19010101' is invalid?
###      -- it works in 64bits system
### 2 sundays fell on first of month at 1901

###
### how many Sundays fell on first of month during 1902 to 2000/12/31
###

###########################################################
## two solutions have different upper limit
start_date="19020101"
end_year="2000"
end_date=$((${end_year}+1))"0101"
sunday_count=0

while (( $start_date < $end_date )) ; do
  if [ $(date -d $start_date +%a) == "Sun" ]; then
    (( sunday_count++ ))
  fi
  start_date=$(date -d "$start_date 1 month" +%Y%m%d)
done

echo $(( $sunday_count + 2 ))

#    start_date=$(date -d $(( $start_date + 100 )) +%Y%m%d)

###########################################################
for i in `seq 1 12`; do for y in `seq 1901 ${end_year}`; do cal $i $y; done ; done | grep '^ 1  2' | wc -l
